from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from house.models import House, District


def house_list(request):
    return render(request, template_name="house_list.html")


def house_edit(request, house_id=None):
    if request.method == "POST":
        house_name = request.POST.get("house_name")
        house_area = request.POST.get("house_area")

        House(title=house_name, area=house_area).save()
        return render(request, "house_list.html")
    else:
        if house_id is not None:
            house = House.objects.filter(_id=house_id).first()
            print(house)
            print(dict(house))

        context = {
            "province_list": ["北京0"],
            "city_list": ["北京1"],
            "county_list": ["北京2"],
            "town_list": ["北京3"],
            "village_list": ["北京4"],
        }
        return render(request, template_name="house_edit.html", context=context)


def house_detail(request, house_id):
    print(house_id)
    print(request)
    return render(request, template_name="house.html")


def get_district(request, parent=None):
    District.objects.filter(parent_id=1)


def district_search(request):
    print(request)
    print(request.GET)
    return JsonResponse({"test": "test"})
